import axios from "axios";
import reqUrl from "./url";
import {ElMessage} from "element-plus"

const instance = axios.create({
	baseURL: reqUrl
})

instance.interceptors.request.use(
	 config => {
	        let formDataInfo;
	        if (config.data instanceof FormData) {
	            formDataInfo = {};
	            for (let [key, value] of config.data) {
	                formDataInfo[key] = value;
	            }
	            console.log(`----请求：${config.method}----路径：${config.url}---- 参数：`, formDataInfo);
	        } else if(config.data){
	            console.log(`----请求：${config.method}----路径：${config.url}---- 参数：`, config.data);
	        } else {
	            console.log(`----请求：${config.method}----路径：${config.url}---- 参数：`, config.params);
	        }
	
	
	        let token = window.localStorage.getItem("token");
	        console.log("设置访问的Token:" + (token ? "存在" : '  ----->>无token  ') + "      访问的url:" + config.url);
	        if (token) {
	            config.headers.Authorization = token;
	        }
	        return config;
	    },
	    error => {
	        return Promise.reject(error);
	    }
)

instance.interceptors.response.use(
	 response => {
	        console.log(response.data)
	        console.log("--------------------↑↑↑↑请求结果↑↑↑↑--------------------------")
	        if (response.data.code == 203) {
	            ElMessage.warning(response.data.msg)
	        }
	        return response;
	    }
	    ,
	    error => {
	        console.log("error 拦截");
	        console.log(error.response.headers)
	        console.log(error.response.data.msg);
	        console.log("出现异常报错,状态码为：" + error.response.status)
	        if (error.response) {
	            switch (error.response.status) {
	                case 401:
	                    ElMessage.error(error.response.data.msg)
	                    window.localStorage.removeItem("role")
	                    window.localStorage.removeItem("name")
	                    window.localStorage.removeItem("img")
	                    window.localStorage.removeItem("account")
	                    window.localStorage.removeItem("token")
	                    store.state.user.account = undefined
	                    store.state.user.img = undefined
	                    store.state.user.name = undefined
	                    store.state.user.role = undefined
	                    router.push("/welcome")
	                    //需要返回登录页
	                    break;
	                case 403:
	                    ElMessage.error(error.response.data.msg)
	                    //无登录权限访问
	                    break;
	                case 203:
	                    ElMessage.error(error.response.data.msg)
	                    console.log(`数据异常：${error.ElMessage}`)
	                    break;
	                case 500:
	                    ElMessage.error(error.response.data.msg)
	                    console.log(`系统开放中.....`)
	                    break;
	                default:
	                    ElMessage.error(error.response.data.msg)
	                    console.log(`数据异常无开发提示...`)
	            }
	        }
	        return Promise.reject(error.response.status)//返回接口的错误信息
	    }
)

const get = (url, obj) => {
    console.log(`正在提交Get请求:${url}`)
    return instance.get(url, { params: obj })
}

const postx = (url, obj) => {
    console.log(`正在提交Post的键值对格式请求:${url}`)
    let o = qs.stringify(obj)
    return instance.post(url, o, {
        header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf8' }
    }
    )
}

const postj = (url, json) => {
    console.log(`正在提交Post的JSON格式请求:${url}`)
    return instance.post(url, json, {
        header: {
            'Content-Type': 'application/json;'
        }
    })
}

const postf = (url, formdata) => {
    console.log(`正在提交Post的FormDate格式请求:${url}`)
    return instance.post(url, formdata, {
        header: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

export default {
    get,
    postx,
    postf,
    postj
}